//
//  HSAwardListVC.m
//  HolyShit
//
//  Created by Robin on 2017/3/31.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSAwardListVC.h"
#import "HSAwardListVM.h"
#import "HSAwardListCell.h"
#import "HSAwardDetailVC.h"
#import "HSAwardPushVC.h"

@interface HSAwardListVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) HSAwardListVM *tagVM;

@end

@implementation HSAwardListVC

- (instancetype)init
{
    if (self = [super init]) {
        self.title = @"彩票";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.tableView];
    self.navigationItem.leftBarButtonItem = nil;
    [self rightTitleWithTitle:@"开奖推送"];
    @weakify(self);
    [self.hintView setReloadBlock:^{
        @strongify(self);
        self.hintView.hintState = MYHintStateLoading;
        [self refresh];
    }];
    self.hintView.reloadBlock();
}

- (void)rightButtonTouch
{
    [self my_push:[HSAwardPushVC new]];
}

- (void)refresh
{
    if (self.tagVM.refreshCommand.isExecuting) {
        return;
    }
    @weakify(self);
    [[[self.tagVM.refreshCommand execute:nil] takeUntil:self.tagVM.rac_willDeallocSignal] subscribeNext:^(id x) {
        @strongify(self);
        self.hintView.hintState = MYHintStateHidden;
        [self.tableView endRefreshAndLoadMore];
        [self.tableView reloadData];
    } error:^(NSError *error) {
        @strongify(self);
        self.hintView.hintState = MYHintStateNoData;
        [self.tableView endRefreshAndLoadMore];
        [UIWindow showError:error];
    }];
}

#pragma mark - UITableView Delegate & Datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.tagVM.topics.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tagVM.topics[section].subInfo.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 35)];
    view.clipsToBounds = YES;
    view.backgroundColor = HEXCOLOR(HSColorBgLight);
    view.top = section == 0 ? -5 : 0;
    
    if(section < self.tagVM.topics.count - 1){
        UILabel *lb = [UILabel labelWithFrame:CGRectMake(0, 5, view.width, 30) font:MYFont(Font30) color:HEXCOLOR(HSColorBlack) text:[NSString stringWithFormat:@"   %@",self.tagVM.topics[section].typename]];
        lb.backgroundColor = WHITE_COLOR;
        [view addSubview:lb];
    
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(3, 10, 3, lb.height - 15)];
        line.backgroundColor = [UIColor redColor];
        [view addSubview:line];
    }
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (CPListType == 2) {
        return 0;
    }
    if(section == self.tagVM.topics.count - 1){
        return 5;
    }
    return 35;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseId = @"HSAwardListCell";
    HSAwardListCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
    if (!cell) {
        cell = [[HSAwardListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];
    }
    cell.model = self.tagVM.topics[indexPath.section].subInfo[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return CPListType == 1 ? 70 : 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    [self my_push:[[HSAwardDetailVC alloc] initWithModel:self.tagVM.topics[indexPath.section].subInfo[indexPath.row]]];
}

- (HSAwardListVM *)tagVM
{
    if (!_tagVM) {
        _tagVM = [HSAwardListVM new];
    }
    return _tagVM;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - NavBarHeight - TabbarHeight)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView addRefreshTarget:self action:@selector(refresh)];
    }
    return _tableView;
}

@end
